<template>
  <div class="container">
    <Breadcrumb :items="['menu.tenant-management', '已删除']" />
    <a-card class="general-card" title="已删除租户">
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="formModel"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="number" label="租户名称">
                  <a-input
                    v-model="formModel.username"
                    placeholder="请输入租户名称……"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 42px" direction="vertical" />
        <a-col :flex="'192px'" style="text-align: right">
          <a-space direction="horizontal" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              {{ $t('searchTable.form.search') }}
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <a-table
        row-key="id"
        :loading="loading"
        :pagination="pagination"
        :data="renderData"
        :bordered="false"
        @page-change="onPageChange"
      >
        <template #columns>
          <a-table-column title="租户名称" data-index="username" />
          <a-table-column title="租户密码" data-index="password" />
          <a-table-column title="租户昵称" data-index="nickname">
          </a-table-column>
          <a-table-column title="手机号" data-index="mobile"> </a-table-column>
          <a-table-column title="邮箱" data-index="email" />
          <a-table-column title="创建时间" data-index="createDate" />
          <a-table-column
            :title="$t('searchTable.columns.operations')"
            data-index="operations"
          >
            <template #cell="record">
              <a-button
                v-permission="['admin']"
                type="outline"
                status="success"
                size="small"
                style="margin-right: 10px"
                @click="handleRecover(record)"
              >
                恢复
              </a-button>
              <a-button
                v-permission="['admin']"
                type="outline"
                status="danger"
                size="small"
                @click="handleDelete(record)"
              >
                清除
              </a-button>
              <a-modal
                v-model:visible="warningVisible"
                @ok="subTruedelete([record.record.id])"
              >
                <template #title> Title </template>
                <div>确定要删除该用户？</div>
              </a-modal>
            </template>
          </a-table-column>
        </template>
      </a-table>

      <!-- <create-zuhu v-model:visible="createVisible"></create-zuhu>
      <modify-zuhu
        v-if="modifyVisible"
        v-model:visible="modifyVisible"
        :dialog-table-value="dialogTableValue"
        @fetchData="fetchData"
      ></modify-zuhu> -->
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { computed, ref, reactive, createElementBlock } from 'vue';
  import { useI18n } from 'vue-i18n';
  import useLoading from '@/hooks/loading';
  import {
    queryPolicyList,
    PolicyRecord,
    PolicyParams,
    recoverUser,
    trueDeleteUser,
  } from '@/api/list';
  import { Pagination } from '@/types/global';
  import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';

  const generateFormModel = () => {
    return {
      username: '',
      name: '',
      contentType: '',
      filterType: '',
      createdTime: [],
      status: '',
    };
  };
  const { loading, setLoading } = useLoading(true);
  const { t } = useI18n();
  const renderData = ref<PolicyRecord[]>([]);
  const formModel = ref(generateFormModel());

  const dialogTableValue = ref({});

  const basePagination: Pagination = {
    page: 1,
    limit: 20,
  };
  const pagination = reactive({
    ...basePagination,
  });
  const role = '2';
  // 提示硬删除的组件
  const warningVisible = ref(false);
  const handleRecover = async (data: any) => {
    await recoverUser([data.record.id]);
  };
  const handleDelete = (data: any) => {
    warningVisible.value = true;
  };
  const subTruedelete = async (data: any) => {
    console.log('执行OK');

    await trueDeleteUser(data);
  };
  const createVisible = ref(false);
  const modifyVisible = ref(false);
  const handleCreate = () => {
    createVisible.value = true;
  };
  const fetchData = async (
    params: PolicyParams = {
      isDeleted: '1',
      page: 1,
      limit: 20,
      role,
      username: formModel.value.username,
    }
    // params
  ) => {
    setLoading(true);
    try {
      console.log(formModel.value.username);
      const { data } = await queryPolicyList(params);
      renderData.value = data.list;
      pagination.page = params.page;
      pagination.total = data.total;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const search = () => {
    fetchData({
      ...basePagination,
      ...formModel.value,
    } as unknown as PolicyParams);
  };
  const onPageChange = (page: number) => {
    fetchData({ ...basePagination, page });
  };

  fetchData();
  const reset = () => {
    formModel.value = generateFormModel();
  };
</script>

<script lang="ts">
  export default {
    name: 'SearchTable',
  };
</script>

<style scoped lang="less">
  .container {
    padding: 0 20px 20px 20px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
